# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Lex Black <autumn-wind@web.de>
# Contributor: Alex Gentilucci <alexander.gentilucci@gmail.com>

pkgname=ft2-clone
pkgver=1.72.1
pkgrel=1
pkgdesc='Portable Fasttracker II clone'
arch=('loong64' 'x86_64')
url='https://16-bits.org/ft2.php'
license=(
  'BSD'
  'MIT'
  'custom:CC-BY-NC-SA-4.0'
)
depends=(
  'glibc'
  'gcc-libs'
  'alsa-lib'
  'sdl2'
  'flac'
  'hicolor-icon-theme'
)
makedepends=(
  'git'
  'libicns'
  'cmake'
  'gendesk'
)
_commit='bb636551437146b801d9c5aa4e71a699975ebefe'
source=(
  "$pkgname::git+https://github.com/8bitbubsy/ft2-clone#commit=$_commit"
  'templatify-build-date.patch'
)
b2sums=('SKIP'
        '5e557ba06426c29898305d4e9ac3872e24d4d0b512a0fd8e75f5c448edec95bd7894015e13e85b3f267f029cdf9f93f8901b8e5c38c3f2184070008065d85f40')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # convert icons
  icns2png -x \
    release/macos/ft2-clone-macos.app/Contents/Resources/ft2-clone-macos.icns

  # generate desktop file
  gendesk -n \
    --exec "$pkgname" \
    --name 'Fasttracker II Clone' \
    --pkgname "$pkgname" \
    --pkgdesc "$pkgdesc" \
    --icon "$pkgname" \
    --genericname 'Chiptune Tracker'

  # attempt repro build by using commit date as build date
  local commit_date="$(git show --no-patch --format=%cd --date=format:'%d %B %Y')"

  # apply template
  patch -p1 -i "$srcdir/templatify-build-date.patch"

  # patch in commit date
  sed -e "s/@DATE@/$commit_date/" -i src/ft2_header.h
}

build() {
  cmake \
    -S "$pkgname" \
    -B build \
    -DEXTERNAL_LIBFLAC=ON \
    -DCMAKE_BUILD_TYPE='None' \
    -DCMAKE_INSTALL_PREFIX='/usr' \
    -Wno-dev

  cmake --build build
}

package() {
  DESTDIR="$pkgdir" cmake --install build

  cd "$pkgname"

  # application icon
  install -vDm644 ft2-clone-macos_512x512x32.png "$pkgdir/usr/share/icons/hicolor/512x512/apps/$pkgname.png"

  # desktop file
  install -vDm644 -t "$pkgdir/usr/share/applications" ft2-clone.desktop

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md

  # licensing
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" release/LICENSES.txt
}
